#!/usr/bin/python

import pygame
import sys
import game_engine.GameEngine


# The entity manager keeps track of all the interesting bits in the game world
class EntityManager:

    # Obvious
    def __init__(self):
        # Init stuff here
        self.m_renderEngine = game_engine.GameEngine.getGameEngine().getRenderEngine()
        self.m_entity = set()
        self.m_playerEntity = None
        self.m_defered_register = set()
        self.m_defered_unregister = set()

    # Obvious
    def cleanup(self):
        return None

    # Update for this frame. Pass in the elapsed time since last frame, as well as absolute game time
    def frameUpdate(self, elapsedms, absolutems):

        # TODO: Update only entities due for update
        # Update all entities
        for entity in self.m_entity:
            entity.frameUpdate(elapsedms, absolutems)

        # Process defered register
        for entity in self.m_defered_register:
            self.m_entity.add(entity)
        self.m_defered_register.clear()
        # Process defered unregister
        for entity in self.m_defered_unregister:
            try:
                self.m_entity.remove(entity)
                game_engine.GameEngine.getGameEngine().getLevelManager().getEntityCollisionMap().removeEntity(entity)
            except KeyError:
                pass
        self.m_defered_unregister.clear()


    # Designate the player entity
    def setPlayerEntity(self, entity):
        self.m_playerEntity = entity

    # Retrieve the player entity
    def getPlayerEntity(self):
        return self.m_playerEntity


    # Add entity to list
    def registerEntity(self, entity, renderPriority):
        self.m_defered_register.add(entity)
        self.m_renderEngine.registerObject(entity, renderPriority)


    # Remove entity from list
    def unregisterEntity(self, entity):
        self.m_defered_unregister.add(entity)
        self.m_renderEngine.unregisterObject(entity)

    # Unregister all entities except the player
    def unregisterAllEntitiesExceptPlayer(self):
        # Unregister all from render engine
        for entity in self.m_entity:
            if entity == self.m_playerEntity:
                continue
            self.unregisterEntity(entity)


    # Spawn a created entity
    def spawnEntity(self, fqcn, entityName, size, animationDataModule, imageDataModule, initialPosition, renderPriority):
        # Ensure module is loaded
        paths = fqcn.split('.')
        moduleName = '.'.join(paths[:-1])
        className = paths[-1]
        # Instantiate
        __import__(moduleName)
        module = sys.modules[moduleName]

        entity = getattr(module, className)(entityName)

        entity.setSize(size[0],size[1])
        if not(animationDataModule is None):
            entity.loadAnimationImage(animationDataModule)
        if not(imageDataModule is None):
            entity.loadImage(imageDataModule)
        entity.setPosition(initialPosition[0], initialPosition[1])

        self.registerEntity(entity, renderPriority)

        return entity



